import java.util.Random;


public class Deck {
	
	public String [] cards = {"Ah ","2h ","3h ","4h ","5h ","6h ","7h ","8h ","9h ","10h ","Jh ","Qh ","Kh ",
			"Ad ","2d ","3d ","4d ","5d ","6d ","7d ","8d ","9d ","10d ","Jd ","Qd ","Kd ",
			"Ac ","2c ","3c ","4c ","5c ","6c ","7c ","8c ","9c ","10c ","Jc ","Qc ","Kc ",
			"As ","2s ","3s ","4s ","5s ","6s ","7s ","8s ","9s ","10s ","Js ","Qs ","Ks "};
	
	public String [] help = new String[0];
	int changedCards=0;
	public  void shuffle (int count) {
		String[]tab=cards;
		int choose;
		String [] cards2; //tablica pomocnicza
		cards2=new String [count];
		Random draw = new Random(); 
		for (int i=0 ; i<count ; i++) {
				do {
					choose=draw.nextInt(count); // losowo wybieranie kart
					cards2[i]=tab[choose];		//sprawdzanie czy byla juz wybrana
					tab[choose]="x";
				} while(cards2[i]=="x");
			
		}
		cards=cards2; // zamiana tablic z niepotasowanej na potasowana
	}
	public String dealing (int amount) {
		String deal="";
		int last=cards.length;
		if (last<4) {
			changeDeck();
			last=cards.length;
		}
		String[] cards3= new String[last-4]; //tablica pomocnicza
		for (int j=0; j<amount; j++) {
			deal=deal+cards[j];  //rozdaj karty
			cards[j]=cards[last-(j+1)]; // podstaw za miejsce kart rozdanych,  karty z konca tablicy
		}
		for(int k=0; k<last-4; k++)
			cards3[k]=cards[k]; // skrocenie tablicy
		cards=cards3; // zamiana tablic
		return deal;	
	}

	public String change (String toChange) {
		String a[]=null;
		a=toChange.split(" ");
		int size=a.length;
		String[] help2=new String [help.length+size];
		for(int i=0; i<help.length; i++)
			help2[i]=help[i];
		int j=0;
		
		for (int i=changedCards; i<size+changedCards; i++) {
			help2[i]=a[j]+" ";
			j++;
		}
		help=help2;
		changedCards=changedCards+size;
		
		String b=dealing(size);
		return b;
	}
	public void changeDeck () {
		int keep=cards.length;
		int k=0;
		if (keep!=0) {
			for (int j=changedCards; j<keep+changedCards; j++) {
				help[j]=cards[k];
				k++;
			}
		}
		cards=help;
		int size=cards.length;
	  shuffle(size);
	  help=new String[0];
	  changedCards=0;
	 }
}
